package com.jinkeen.xiaxie.chatRoom

import android.content.Context
import android.media.Image
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.jinkeen.xiaxie.R

class ChatRoomAdapter : RecyclerView.Adapter<ChatRoomAdapter.MyViewHolder> {

    private var context: Context? = null
    private var chatModels: ArrayList<ChatModel>? = null

    constructor(context: Context, chatModels: ArrayList<ChatModel>) {
        this.chatModels = chatModels
        this.context = context
        notifyDataSetChanged()
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        return MyViewHolder(LayoutInflater.from(context).inflate(R.layout.list_chat_item,parent,false))
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        if ("send" == chatModels!![position].type){
            holder.leftLayout.visibility=View.GONE
            holder.rightContent.text=chatModels!!.get(position).contnet
            holder.rightName.text=chatModels!!.get(position).name
            holder.rightImg.setImageResource(R.mipmap.ic_launcher_round)
        }else{
            holder.rightLayout.visibility=View.GONE
            holder.leftContent.text= chatModels!![position].contnet
            holder.leftName.text= chatModels!![position].name
            holder.leftImg.setImageResource(R.mipmap.ic_launcher_round)
        }
    }

    override fun getItemCount()=chatModels!!.size

    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var leftImg=itemView.findViewById<ImageView>(R.id.left_image)
        var leftName=itemView.findViewById<TextView>(R.id.left_name)
        var leftContent=itemView.findViewById<TextView>(R.id.left_content)
        var leftLayout=itemView.findViewById<LinearLayout>(R.id.left_bubble)

        var rightImg=itemView.findViewById<ImageView>(R.id.right_image)
        var rightName=itemView.findViewById<TextView>(R.id.right_name)
        var rightContent=itemView.findViewById<TextView>(R.id.right_content)
        var rightLayout=itemView.findViewById<LinearLayout>(R.id.right_bubble)
    }
}